package com.example.batch_demo.controller;

import com.example.batch_demo.entity.source.StudentSource;
import com.example.batch_demo.entity.target.StudentTarget;
import com.example.batch_demo.repository.source.StudentSourceRepository;
import com.example.batch_demo.repository.target.StudentTargetRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description
 * @Author lihw
 * @Date 2023/12/14 15:09
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    StudentSourceRepository studentSourceRepository;

    @Autowired
    StudentTargetRepository studentTargetRepository;

    @Autowired
    @Qualifier("sourceEntityManager")
    EntityManager entityManager;

    @Autowired
    @Qualifier("targetEntityManager")
    EntityManager targetEntityManager;

    @GetMapping("db1")
    public void testDb1() {
        Iterable<StudentSource> all = studentSourceRepository.findAll(Sort.unsorted());
        log.info("source: {}", all);

        Iterable<StudentTarget> all1 = studentTargetRepository.findAll(Sort.unsorted());
        log.info("target: {}", all1);

        Query selectSFromStudentSourceS = entityManager.createQuery("select s from StudentSource s");
        List resultList = selectSFromStudentSourceS.getResultList();
        log.info("resultList: {}", resultList);


    }

    @GetMapping("db2")
    public void testDb2() {
        Query selectSFromStudentSourceS2 = targetEntityManager.createQuery("select s from StudentTarget s");
        List resultList2 = selectSFromStudentSourceS2.getResultList();
        log.info("resultList2: {}", resultList2);
    }

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    @Qualifier("studentJpaSyncJob")
    Job studentJpaSyncJob;

    @GetMapping("sync1")
    public void testSync() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobParameters jobParameters = new JobParametersBuilder()
                .addLong("key", System.currentTimeMillis())
                .toJobParameters();

        JobExecution run = jobLauncher.run(studentJpaSyncJob, jobParameters);
        if (run.getStatus() == BatchStatus.COMPLETED) {
            log.info("studentJpaSyncJob 》》》 执行完成");
        } else {
            log.info("studentJpaSyncJob 》》》 执行失败");
        }
    }
}
